/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import entity.RegisteredUser;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import entity.Contact;
import java.util.List;
import entity.enumeration.InvitationStatus;
import java.io.Serializable;
import java.util.LinkedList;

/**
 * 
 * @author Stefan
 */
public class RegisteredUserManager extends DefaultManager<RegisteredUser> implements Serializable {

    public RegisteredUserManager() {
        super(RegisteredUser.class);
    }

    public RegisteredUser validateLogin(String username, String password) {
        String hashPass = password;//HashGenerator.generateMD5(password.trim());
        RegisteredUser retVal = null;

        EntityManager em = getEntityManager();
        String query = "SELECT x FROM RegisteredUser x WHERE x.deleted = 0 AND x.email = ?1 AND x.password_hash = ?2 AND x.verified = 1";
        Query q = em.createQuery(query);
        q.setParameter(1, username);
        q.setParameter(2, hashPass);
        System.out.println("Hash: " + hashPass);
        List results = q.getResultList();
        if (!results.isEmpty()) {
            retVal = (RegisteredUser) results.get(0);
        }

        em.close();
        return retVal;
    }

    public RegisteredUser getRegisteredUserByMail(String email) {
        RegisteredUser retVal = null;
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM RegisteredUser x WHERE x.deleted = 0 AND x.email = ?1";
        Query q = em.createQuery(query);
        q.setParameter(1, email);
        List<RegisteredUser> results = q.getResultList();
        if (!results.isEmpty()) {
            retVal = results.get(0);
        }

        em.close();
        return retVal;
    }

    public int getPendingInvitationsCount(RegisteredUser user) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Invitation x WHERE x.contact.email = ?1 AND x.status = ?2 ";
        Query q = em.createQuery(query);
        q.setParameter(1, user.getEmail());
        q.setParameter(2, InvitationStatus.Pending);
        int retVal = q.getResultList().size();
        em.close();
        return retVal;
    }

    public List<Contact> getNonDeletedContacts(RegisteredUser user) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM Contact x WHERE x.registeredUser = ?1 AND x.deleted = false ";
        Query q = em.createQuery(query);
        q.setParameter(1, user);
        List res = q.getResultList();
        List<Contact> ret = new LinkedList<Contact>();
        for (Object r : res) {
            ret.add((Contact) r);
        }
        return ret;
    }

    public RegisteredUser activateUser(String code) {
        EntityManager em = getEntityManager();
        String query = "SELECT x FROM RegisteredUser x WHERE x.verificationCode = ?1 AND x.deleted = 0 ";
        Query q = em.createQuery(query);
        q.setParameter(1, code);
        List<RegisteredUser> res = q.getResultList();
        if (!res.isEmpty()) {
            RegisteredUser ru = res.get(0);
            ru.setVerified(true);
            try {
                edit(ru);
                return ru;
            } catch (Exception e) {
                return null;
            }
        }

        return null;
    }
}
